//
//  AudioPlayerExampleViewController.m
//  AudioPlayerExample
//

#import "AudioPlayerExampleViewController.h"

@implementation AudioPlayerExampleViewController

- (void)resetControls {
	audioPlayer.currentTime = 0;
	scrubber.value = 0;
	[playButton setTitle:@"Play" forState:UIControlStateNormal];
}

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player {
	interrupted = audioPlayer.playing;
}

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player {
	if (interrupted) {
		audioPlayer.play;
	}
}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player
					   successfully:(BOOL)flag {
	[self resetControls];
}

- (void)play:(id)sender {
	if (!audioPlayer.playing) {
		audioPlayer.play;
		[playButton setTitle:@"Pause" forState:UIControlStateNormal];
	}
	else {
		audioPlayer.pause;
		[playButton setTitle:@"Play" forState:UIControlStateNormal];
	}
}

- (void)stop:(id)sender {
	audioPlayer.stop;
	audioPlayer.currentTime = 0;
	[playButton setTitle:@"Play" forState:UIControlStateNormal];
}

- (void)changeVolume:(id)sender {
	audioPlayer.volume = volume.value;
}

- (void)scrub:(id)sender {
	if (audioPlayer.playing) {
		audioPlayer.pause;
		audioPlayer.currentTime = scrubber.value;
		audioPlayer.play;
	}
	else {
		audioPlayer.currentTime = scrubber.value;
	}

}

- (void)createControls {
	playButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[playButton setFrame:CGRectMake(60, 10, 80, 34)];
	[playButton setTitle:@"Play" forState:UIControlStateNormal];
	[playButton addTarget:self action:@selector(play:)
		 forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:playButton];
	
	stopButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
	[stopButton setFrame:CGRectMake(180, 10, 80, 34)];
	[stopButton setTitle:@"Stop" forState:UIControlStateNormal];
	[stopButton addTarget:self action:@selector(stop:)
		 forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview:stopButton];
	
	volume = [[UISlider alloc] initWithFrame:CGRectMake(60, 80, 200, 0)];
	[volume addTarget:self action:@selector(changeVolume:)
	 forControlEvents:UIControlEventValueChanged];
	volume.minimumValue = 0.0;
	volume.maximumValue = 1.0;
	volume.value = audioPlayer.volume;
	volume.continuous = YES;
	[self.view addSubview:volume];
	
	scrubber = [[UISlider alloc] initWithFrame:CGRectMake(60, 140, 200, 0)];
	[scrubber addTarget:self action:@selector(scrub:)
	   forControlEvents:UIControlEventValueChanged];
	scrubber.minimumValue = 0.0;
	scrubber.maximumValue = audioPlayer.duration;
	scrubber.value = audioPlayer.currentTime;
	scrubber.continuous = NO;
	[self.view addSubview:scrubber];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSString *filePath = [[[NSBundle mainBundle] resourcePath]
						  stringByAppendingPathComponent:@"song.mp3"];
	NSURL *fileURL = [NSURL fileURLWithPath:filePath];
	
	NSError *error = nil;
	audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
	if (error)
		NSLog(@"Error: %@", error);
	else {
		audioPlayer.volume = 0.3;
		[audioPlayer prepareToPlay];
		[self createControls];
	}

		audioPlayer.play;
}

- (void)dealloc {
	[scrubber release];
	[volume release];
	[audioPlayer release];
    [super dealloc];
}

@end
